#!/bin/tcsh -ef
####
#
#############################################################################
#                                                                           #
# Title: Prepare Tiles (EXPERIMENTAL)                                       #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 01/12/2016                                             #
# Last Modification: 01/12/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 90
#
# MANUAL: This script allows to launch several jobs in parallel.  Choose below, which 2dx_image scripts to include.
#
# DISPLAY: ReUnbend_MP
# DISPLAY: tile_copy_merge
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set ReUnbend_MP = ""
set tile_copy_merge = ""
#
#$end_vars 
#
set scriptname = 2dx_tile
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
source ${proc_2dx}/2dx_merge_makedirs 
#
echo "<<@progress: 1>>"
#
echo "# IMAGE: 2dx_merge_dirfile.dat <TXT: Directory List>" >> LOGS/${scriptname}.results
echo "# IMAGE: LOGS/${scriptname}_sub.logfile <TXT: Output of subscripts>" >> LOGS/${scriptname}.results
#
set newimagesidelength = 1024
set tilenumber = 5
#
set olddir = ${PWD}
cd ..
set rootdir = ${PWD}
#
############################################################################# 
# Copy Merge Directory to new TILE location
############################################################################# 
if ( ! -d TILES ) then
  \mkdir TILES
endif
cd TILES
set target_base = $PWD
echo "::Target base is ${target_base}"
if ( ! -d merge ) then
  \mkdir merge
  \cp -r ${olddir}/* merge
else
  if ( ${tile_copy_merge} == "1" ) then
    \cp -rf ${olddir}/* merge
  endif
endif
############################################################################# 
#
cd ${olddir} 
#
# set loadaverage = `uptime | cut -d\: -f4 | cut -d\  -f2`
set loadaverage = `ps -axf | sed -n '/${scriptname}/p' | wc -l`
set ReUnbend_MP = `cat 2dx_merge.cfg | grep "set ReUnbend_MP" | cut -d\" -f2`
\cp -f 2dx_merge.cfg SCRATCH/${scriptname}_2dx_merge_cfg.tmp
#
set dirnum = `cat 2dx_merge_dirfile.dat | wc -l`
echo ": Will work on ${dirnum} image directories."
#
\rm -f LOGS/${scriptname}_sub.logfile
\cp -f 2dx_merge_dirfile.dat SCRATCH/${scriptname}_2dx_merge_dirfile_runfile.dat
#
set dircounter = 1
while ( ${dircounter} <= ${dirnum} )
    #
    set prog_num = `echo ${dircounter} ${dirnum} | awk '{ s = 5 + int( 90 * $1 / $2 ) } END { print s }'` 
    echo "<<@progress: ${prog_num}>>"
    #
    cd ${olddir}
    ${bin_2dx}/2dx_getline.exe << eot > TMP.tmp
SCRATCH/${scriptname}_2dx_merge_dirfile_runfile.dat
${dircounter}
eot
    #
    set from_dir = `cat TMP.tmp`
    \rm TMP.tmp
    #
    cd ..
    cd ${from_dir}
    #
    if ( -e 2dx_image.cfg ) then
      echo ": "
      echo "Working on ${from_dir} ("`date`")"

      ${proc_2dx}/linblock "At load ${loadaverage}/${ReUnbend_MP}, launching job ${dircounter} of ${dirnum} on ${from_dir}"

      ###########################################################################################################################
      # set bin_2dx = $1
      # set proc_2dx = $2
      # set SCRATCH_DISK = $3
      # set scriptname = $4
      # set newimagesidelength = $5
      # set tilenumber = $6
      # set olddir = $7
      # set from_dir = $8
      #
      echo ": Launching: "
      echo ":" ${proc_2dx}/2dx_tile_sub.com ${bin_2dx} ${proc_2dx} ${SCRATCH_DISK} ${scriptname} ${newimagesidelength} ${tilenumber} ${olddir} ${from_dir}
      #
      nohup ${proc_2dx}/2dx_tile_sub.com ${bin_2dx} ${proc_2dx} ${SCRATCH_DISK} ${scriptname} ${newimagesidelength} ${tilenumber} ${olddir} ${from_dir} >> ${olddir}/LOGS/${scriptname}_sub.logfile &
      #
      ###########################################################################################################################

      cd ${olddir}
      set load_too_high = 1
      while ( ${load_too_high} == 1 ) 
        nohup ${proc_2dx}/getload.com &
        sleep 1
        if ( -e SCRATCH/getload.dat ) then
          set loadaverage = `cat SCRATCH/getload.dat`
        else
          sleep 1
          set loadaverage = 50
        endif
        set load_too_high = `echo ${loadaverage} ${ReUnbend_MP} | awk '{ if ( ( $1 - 0 ) > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
        if ( ${load_too_high} == 1 ) then
          sleep 4
        endif
      end

    else
      echo "::ERROR for ${from_dir}: No 2dx_image.cfg found."
    endif
    # 
    # 
    @ dircounter++
    cd ${olddir}
  end
  cd ${olddir}
  echo "::Waiting for last jobs to finish"
  set load_too_high = 1
  while ( ${load_too_high} == 1 ) 
    nohup ${proc_2dx}/getload.com &
    sleep 1
    if ( -e SCRATCH/getload.dat ) then
      set loadaverage = `cat SCRATCH/getload.dat`
      # echo "::loadaverage = ${loadaverage}"
    else
      sleep 1
      set loadaverage = 50
    endif
    set load_too_high = `echo ${loadaverage} 1 | awk '{ if ( ( $1 - 2 ) > $2 ) { s = 1 } else { s = 0 } } END { print s }'`
    if ( ${load_too_high} == 1 ) then
      sleep 5
    endif
  end
  echo "::Last jobs have finished."
#
#
echo ":: Done."
echo "<<@progress: 100>>"
#
############################################################################# 
${proc_2dx}/linblock "${scriptname} - normal end."
#############################################################################
#
exit
#
source ${proc_2dx}/getload.com
source ${proc_2dx}/2dx_tile_sub.com
source ${proc_2dx}/2dx_tile_sub_sub.com
#
